package com.mnotice.message.factory;


import com.mnotice.common.constants.InformerEnum;
import com.mnotice.common.entity.message.Notification;
import com.mnotice.message.informer.Informer;
import com.mnotice.message.informer.impl.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class InformerFactory {
    private static final Map<InformerEnum, Informer> informers = new HashMap<>();
    @Resource
    private WxProgramInformer wxProgramInformer;
    @Resource
    private EmailInformer emailInformer;
    @Resource
    private SmsInformer smsInformer;
    @Resource
    private WebInformer webInformer;
    @Resource
    private DefaultInformer defaultInformer;

    public Informer getInformer(Notification notification) {
        return getInformer(notification.getInformMethod());
    }

    public Informer getInformer(InformerEnum informMethod) {
        if (InformerEnum.WeiXin.equals(informMethod)) {
            return informers.computeIfAbsent(informMethod, method -> wxProgramInformer);
        } else if (InformerEnum.Email.equals(informMethod)) {
            return informers.computeIfAbsent(informMethod, method -> emailInformer);
        } else if (InformerEnum.ShortMessage.equals(informMethod)) {
            return informers.computeIfAbsent(informMethod, method -> smsInformer);
        } else if (InformerEnum.WebSite.equals(informMethod)) {
            return informers.computeIfAbsent(informMethod, method -> webInformer);
        }
        return informers.computeIfAbsent(informMethod, method -> defaultInformer);
    }
}
